<template>
  <div class="page-next">
    <Row type="flex" justify="center">
      <Col span="16" v-if="!isVerify">
        <Row type="flex" justify="center">
          <Col span="16">
            <Form
              ref="FromRef"
              :model="fromData"
              :rules="fromRule"
              label-position="top"
              @on-validate="formValidate"
            >
              <FormItem label="手机号码" prop="phoneNum">
                <Input
                  v-model="fromData.phoneNum"
                  maxlength="11"
                  show-word-limit
                  clearable
                  placeholder="请输入内容"
                ></Input>
              </FormItem>
              <FormItem label="验证码" prop="smsCode">
                <div class="verify">
                  <Input
                    v-model="fromData.smsCode"
                    placeholder="请输入验证码"
                  ></Input>
                  <div class="verify-box">
                    <slide-verify ref="slideVerify" @captchaFn="captchaFn"
                      ><span @click="getVerifyCode()">
                        {{ isShow ? "获取验证码" : count + "s" }}
                      </span></slide-verify
                    >
                  </div>
                </div>
              </FormItem>
            </Form>
            <div class="next-btn">
              <Button
                type="primary"
                :disabled="isBtnDisabled"
                @click="handleAgree"
                >下一步</Button
              >
            </div>
          </Col></Row
        >
      </Col>
      <Col span="16" v-else>
        <RegistrFinish
          tipsIcon="ios-close-circle-outline"
          tipsName="验证失败"
          tipsContent="您的手机号不正确"
          tipsBtnText="重新验证"
          tipsBtnType="2"
          @submitBtn="submitBtn"
        />
      </Col>
    </Row>
  </div>
</template>
<script type="text/javascript">

import {
  SendVerifyMobileSms
} from "@/api/common.js";

import slideVerify from "@/components/slideVerify";

export default {
  components: {
    slideVerify,
  },
  props: {
    types: {
      type: Number,
      default: 2,
    },
  },
  data() {
    const validatePhone = (rule, value, callback) => {
      if (value) {
        let re = /^1\d{10}$/; // 验证中文
        if (re.test(value)) {
          callback();
        } else {
          callback(new Error("请输入正确的手机号"));
        }
      } else {
        callback(new Error("请输入手机号"));
      }
    };

    return {
      isBtnDisabled: true,
      fromData: {
        phoneNum: "",
        smsCode: "",
      },
      fromRule: {
        phoneNum: [
          { required: true, message: "请输入手机号", trigger: "change" },
          { validator: validatePhone, trigger: "change" },
        ],
        smsCode: [{ required: true, message: "请输入验证码", trigger: "change" }],
      },
      interval: null,
      count: 0,
      isShow: true,
      verifyPassword: 1,
      isVerify: false,
    };
  },
  methods: {
    handleAgree() {
      // if (1) {
      //   this.isVerify = true;
      //   return;
      // }
      localStorage.setItem('updatePwdPhoneNum',this.fromData.phoneNum)
      this.$emit("handleAgree", this.$props.types);
    },
    formValidate(prop, status, error) {
      let isValidate = false;
      console.log(this.fromData, "this.fromData");
      for (let item in this.fromData) {
        if (!this.fromData[item]) {
          console.log(1111,item,this.fromData[item])
          isValidate = true;
          break;
        }
      }
      this.isBtnDisabled = isValidate;
    },
    getVerifyCode() {
      if(!this.fromData.phoneNum){
        this.$Message.error("请输入手机号")
        return
      }
      if (this.count != 0) {
        return;
      }
      this.$refs.slideVerify.innuser();
    },
    async captchaFn() {
      let params = {
        xOffset: 20,
      };
      // let data = await getSlideVerifyImage();
      // let xOffsetData = await ValidSlideVerifyImage(params);
      let verifyParams = {
        mobile: this.fromData.phoneNum,
      };
      try {
        let verifyCode = await SendVerifyMobileSms(verifyParams);
        console.log("data", verifyCode);
        this.$Message.success("短信发送成功！")
        this.isShow = false;
        this.count = 60;
        this.interval = setInterval(() => {
          if (this.count != 0) {
            this.count--;
          } else {
            this.isShow = true;
          }
        }, 1000);
      } catch (error) {}
    },
    submitBtn() {
      this.isVerify = false;
    },
  },
  mounted() {},
};
</script>

<style lang="less" scoped>
.next-btn {
  display: flex;
  justify-content: center;
}
.ivu-btn {
  width: 100%;
}
.pwd-box {
  flex: none;
  display: flex;
  align-items: center;
  padding-top: 10px;
  .pwd-box-tips {
    font-size: 12px;
    color: #aaa;
    line-height: 1;
    margin-left: 10px;
  }
}
.pwd-box-item {
  display: flex;
  span {
    display: block;
    width: 60px;
    height: 4px;
    background: #c4c4c4;
    margin-right: 5px;
  }
  span.pwd-box-current1 {
    background: #ff0000;
  }

  span.pwd-box-current2 {
    background: #ffbf6b;
  }

  span.pwd-box-current3 {
    background: #a2ef4d;
  }
}

.verify {
  position: relative;
  .verify-box {
    position: absolute;
    right: 5px;
    top: 4px;
    cursor: pointer;
  }
}
</style>
